浏览 1507 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-01-14
//use recursion public static void mirrorHelp1(Node node){ if(node==null)return; swapChild(node); mirrorHelp1(node.getLeft()); mirrorHelp1(node.getRight()); } //use no recursion but stack public static void mirrorHelp2(Node node){ if(node==null)return; Stack<Node> stack=new Stack<Node>(); stack.add(node); while(!stack.isEmpty()){ node=stack.pop(); swapChild(node); if(node.getLeft()!=null){ stack.push(node.getLeft()); } if(node.getRight()!=null){ stack.push(node.getRight()); } } } public static void swapChild(Node node){ /*not like c/c++,you cannot do this: Node temp=left; left=right; right=temp; */ Node left=node.getLeft(); node.setLeft(node.getRight()); node.setRight(left); } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |