赞
踩
前序,中序和后序除递归方法外,推荐前序非递归3,中序非递归2,后序非递归(这个以后要回来看,明天怕是就会忘掉),层序以后可以关注一下有没有递归的方法。
- class TreeNode{
- int val;
- TreeNode left,right;
- public TreeNode(int val) {
- super();
- this.val = val;
- }
- public TreeNode(int val, TreeNode left, TreeNode right) {
- super();
- this.val = val;
- this.left = left;
- this.right = right;
- }
- //前序递归
- public static String preOrder(TreeNode root) {
- StringBuilder builder=new StringBuilder();
- preOrderHelper(root, builder);
- return builder.toString();
- }
- public static void preOrderHelper(TreeNode n,StringBuilder builder) {
- if(n==null)return;
- //如果遇到需要判断叶子结点的需要以下代码
- // TreeNode left=n.left;
- // TreeNode right=n.right;
- // if(left==null&&right==null)return;
- builder.append(n.val+" ");
- preOrderHelper(n.left, builder);
- preOrderHelper(n.right,builder);
-
- }
- //前序非递归1
- public static String preOrderNonRecursive1(TreeNode root) {
- StringBuilder builder=new StringBuilder();
- Stack<TreeNode> stack=new Stack<>();
- stack.push(root);
- while(stack.size()!=0) {
- TreeNode n=stack.pop();
- if(n==null)continue;
- builder.append(n.val+" ");
- stack.push(n.right);
- stack.push(n.left);
- }
- return builder.toString();
- }
-
- //前序非递归2
- public static String preOrderNonRecursive2(TreeNode root) {
- StringBuilder builder=new StringBuilder();
- Stack<TreeNode> stack=new Stack<>();
- while(true) {
- while(root!=null) {
- builder.append(root.val+" ");
- stack.push(root);
- root=root.left;
- }
- if(stack.isEmpty())break;
- root=stack.pop();
- root=root.right;
- }
- return builder.toString();
- }
- //前序非递归写法3
- public static String preOrderNonRecursive3(TreeNode root) {
- StringBuilder builder=new StringBuilder();
- Stack<TreeNode> stack=new Stack<>();
- while(root!=null||!stack.isEmpty()) {
- if(root!=null) {
- stack.push(root);
- builder.append(root.val);
- root=root.left;
- }
- else {
- root=stack.pop();
- root=root.right;
- }
- }
- return builder.toString();
- }
-
- //中序遍历递归
- public static String inOrderRecursive(TreeNode root) {
- StringBuilder builder=new StringBuilder();
- inOrderHelper(root, builder);
- return builder.toString();
- }
- public static void inOrderHelper(TreeNode n,StringBuilder builder) {
- if(n==null)return;
- inOrderHelper(n.left, builder);
- builder.append(n.val+" ");
- inOrderHelper(n.right, builder);
- }
- //中序遍历非递归写法1
- public static String inOrderNonRecursive(TreeNode root) {
- Stack<TreeNode> stack=new Stack<>();
- StringBuilder builder=new StringBuilder();
- while(true) {
- while(root!=null) {
- stack.push(root);
- root=root.left;
- }
- if(stack.isEmpty())break;
- root=stack.pop();
- builder.append(root.val+" ");
- root=root.right;
- }
- return builder.toString();
- }
- //中序非递归写法2
- public static String inOrderNonRecursive2(TreeNode root) {
- StringBuilder builder=new StringBuilder();
- Stack<TreeNode> stack=new Stack<>();
- while(root!=null||!stack.isEmpty()) {
- if(root!=null) {
- stack.push(root);
- root=root.left;
- }else {
- root=stack.pop();
- builder.append(root.val);
- root=root.right;
- }
- }
- return builder.toString();
- }
- //后序递归
- public static String postOrderRecursive(TreeNode root) {
- StringBuilder builder=new StringBuilder();
- postOrderHelper(root, builder);
- return builder.toString();
- }
- public static void postOrderHelper(TreeNode n,StringBuilder builder) {
- if(n==null)return;
- postOrderHelper(n.left, builder);
- postOrderHelper(n.right, builder);
- builder.append(n.val);
- }
- //后序非递归
- public static String postOrderNonRecursive(TreeNode root) {
- Stack<TreeNode> stack=new Stack<>();
- StringBuilder builder=new StringBuilder();
- TreeNode pre=null,curr;
- stack.push(root);
- while(!stack.isEmpty()) {
- curr=stack.peek();
- if((curr.left==null&&curr.right==null)||(pre!=null&&(pre==curr.left||pre==curr.right))) {
- builder.append(curr.val);
- stack.pop();
- pre=curr;
- }else {
- if(curr.right!=null)
- stack.push(curr.right);
- if(curr.left!=null)
- stack.push(curr.left);
- }
- }
- return builder.toString();
- }
- //层序遍历好像还没有递归的方法,因为他一层一层,有很多结点。
- public static String BFS(TreeNode root) {
- StringBuilder builder=new StringBuilder();
- LinkedList<TreeNode> queue=new LinkedList<>();
- queue.offer(root);
- while(!queue.isEmpty()) {
- TreeNode n=queue.poll();
- if(n==null)continue;
- builder.append(n.val);
- queue.offer(n.left);
- queue.offer(n.right);
- }
- return builder.toString();
- }
-
- @Override
- public String toString() {
- return "TreeNode [val=" + val +"]";
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。