144-94-145 二叉树的前中后序遍历

来源:互联网 发布:淘宝怎样才能分期付款 编辑:程序博客网 时间:2024/05/29 17:44

前序遍历递归解法


public class Solution {    public List<Integer> preorderTraversal(TreeNode root) {        List<Integer>list =new ArrayList<>();        preorder(root,list);        return list;    }    public void preorder(TreeNode root,List<Integer> list){        if(root==null) return ;        if(root!=null){            list.add(root.val);        }        preorder(root.left,list);        preorder(root.right,list);    }}

先序遍历非递归解法

先把所有左节点的值放到stack中 同时把左节点的值放入list,此时已经把所有最左侧的节点放入list  然后逐个弹出 弹出时遍历它右节点(子树)


public class Solution {    public List<Integer> preorderTraversal(TreeNode root) {          List<Integer> list = new ArrayList<Integer>();          Stack<TreeNode> stack = new Stack<TreeNode>();                  while( !stack.isEmpty() || root!=null){              while(root != null){                  stack.push(root);                list.add(root.val);                root = root.left;              }                            if( !stack.isEmpty()){                  root = stack.pop();                  // list.add(root.val);                  root = root.right;              }          }                    return list;      }  }



中序遍历的递归解法

public class Solution {    public List<Integer> inorderTraversal(TreeNode root) {        List<Integer> list=new ArrayList<>();        inorder(root,list);        return list;    }    public void inorder(TreeNode root,List<Integer> list){        if(root==null) return ;        if(root.left!=null)        {           inorder(root.left,list);        }         list.add(root.val);            if(root.right!=null)                inorder(root.right,list);    }}


中序遍历的非递归解法

先把左右的左节点放到stack中,然后一个一个pop出来 pop的同时检查对应的右节点

public class Solution {    public List<Integer> inorderTraversal(TreeNode root) {          List<Integer> list = new ArrayList<Integer>();          Stack<TreeNode> stack = new Stack<TreeNode>();                  while( !stack.isEmpty() || root!=null){              while(root != null){                  stack.push(root);                  root = root.left;              }                            if( !stack.isEmpty()){  // 当遇到节点为空时已经遍历完了此部分,需要再弹出一个节点进行遍历                root = stack.pop();                  list.add(root.val);                  root = root.right;              }          }                    return list;      }  }



后序遍历的递归解法

addfirst() 每次将节点插在第一的位置而不是插在末尾

public class Solution {    public List<Integer> postorderTraversal(TreeNode root) {        List<Integer> list=new ArrayList<>();        postorder(root,list);        return list;    }    public void postorder(TreeNode root,List<Integer> list){        if(root==null) return;        if(root.left!=null){            postorder(root.left,list);        }        if(root.right!=null){            postorder(root.right,list);        }        list.add(root.val);    }}

后序遍历非递归解法

public class Solution {    public List<Integer> postorderTraversal(TreeNode root) {          LinkedList<Integer> list = new LinkedList<Integer>();          Stack<TreeNode> stack = new Stack<TreeNode>();                  while( !stack.isEmpty() || root!=null){              while(root != null){                  stack.push(root);                    list.addFirst(root.val);                  root = root.right;              }                            if( !stack.isEmpty()){                                  root = stack.pop();                              root = root.left;              }          }                    return list;      }  }



0 0
原创粉丝点击