LeetCode—二叉树的遍历

1.二叉树的前序遍历


给出一棵二叉树,返回其节点值的前序遍历。

样例

给出一棵二叉树 {1,#,2,3},

   1
    \
     2
    /
   3

 返回 [1,2,3].

代码:


 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Preorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        
         ArrayList<Integer> res=new ArrayList<Integer>();
       
        Stack<TreeNode> stack=new Stack<>();
        if(root!=null){
            stack.push(root);
            while(stack.size()>0){
            	TreeNode t=stack.pop();
                res.add(t.val);
                if(t.right!=null){
                    stack.push(t.right);
                }
                
                if(t.left!=null){
                    stack.push(t.left);
                }
            }
        }
        return res;
    }
}

2.二叉树的中序遍历


给出一棵二叉树,返回其中序遍历

样例

给出二叉树 {1,#,2,3},

   1
    \
     2
    /
   3

返回 [1,3,2].

代码:

 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Inorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
       
         ArrayList<Integer> res=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||stack.size()>0){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            if(stack.size()>0){
                cur=stack.pop();
                res.add(cur.val);
                cur=cur.right;
               
            }
        }
        return res;
    }
}

3.
二叉树的后序遍历


给出一棵二叉树,返回其节点值的后序遍历。

样例

给出一棵二叉树 {1,#,2,3},

   1
    \
     2
    /
   3

返回 [3,2,1]

代码:

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Postorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
       
        ArrayList<Integer> res=new ArrayList<Integer>();
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        TreeNode prev=root;//标记节点已经被访问了
        while(cur!=null||stack.size()>0){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
                
            }
            if(stack.size()>0){
                TreeNode right=stack.peek().right;
                if(right==null||right==prev){
                    cur=stack.pop();
                    res.add(cur.val);
                    prev=cur;
                    cur=null;
                }
                else{
                    cur=right;
                }
            }
        }
        return res;
    }
}

4.二叉树的层次遍历

给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问)

样例

给出一棵二叉树 {3,9,20,#,#,15,7},

   

    3
   / \
  9  20
    /  \
   15   7

按照从上往下的层次遍历为:

[
  [3],
  [9,20],
  [15,7]
]

代码:

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
 
 
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Level order a list of lists of integer
     */
    public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        
        ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
        
		 ArrayList<Integer> list=null;
		 Queue<TreeNode> queue=new LinkedList<TreeNode>();
		 if(root!=null){
			 queue.add(root);
			 while(queue.size()>0){
				 int size=queue.size();
				 list=new ArrayList<Integer>();
				 for(int i=0;i<size;i++){
					 TreeNode t=queue.poll();
					 list.add(t.val);
					 if(t.left!=null)
						 queue.add(t.left);
					 if(t.right!=null)
						 queue.add(t.right);
				 }
				 res.add(list);
			 }
		 }
		 return res;
    }
}

5.二叉树的层次遍历 II

给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历)

样例

给出一棵二叉树 {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

按照从下往上的层次遍历为:

[
  [15,7],
  [9,20],
  [3]
]
代码:

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
 
 
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: buttom-up level order a list of lists of integer
     */
    public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
       
        ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
        if(root==null){
            return res;
        }
        ArrayList<ArrayList<Integer>> arr=levelOrder(root);
        for(int i=arr.size()-1;i>=0;i--){
           ArrayList<Integer> list=new ArrayList<Integer>();
           for(int j=0;j<arr.get(i).size();j++){
               list.add(arr.get(i).get(j));
           }
           res.add(list);
        }
        return res;
    }
    public static ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        // write your code here
        ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
        
		 ArrayList<Integer> list=null;
		 Queue<TreeNode> queue=new LinkedList<TreeNode>();
		 if(root!=null){
			 queue.add(root);
			 while(queue.size()>0){
				 int size=queue.size();
				 list=new ArrayList<Integer>();
				 for(int i=0;i<size;i++){
					 TreeNode t=queue.poll();
					 list.add(t.val);
					 if(t.left!=null)
						 queue.add(t.left);
					 if(t.right!=null)
						 queue.add(t.right);
				 }
				 res.add(list);
			 }
		 }
		 return res;
    }
}

6.二叉树的锯齿形层次遍历

给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行)

样例

给出一棵二叉树 {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

返回其锯齿形的层次遍历为:

[
  [3],
  [20,9],
  [15,7]
]
代码:

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
 
 
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: A list of lists of integer include 
     *          the zigzag level order traversal of its nodes' values 
     */
    public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
        
        ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> list=null;
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        
        if(root!=null){
            queue.add(root);
           int count=1;//记录树的层数,根节点为第一层
            while(queue.size()>0){
                int size=queue.size();
                list=new ArrayList<>();
                for(int i=0;i<size;i++){
                    TreeNode t=queue.poll();
                    if(count%2==0){
                        list.add(0,t.val);//关键,头插法
                    }
                    
                    else{
                        list.add(t.val);
                    }
                    if(t.left!=null){
                        queue.add(t.left);
                    }
                    if(t.right!=null){
                        queue.add(t.right);
                    }
                }
                res.add(list);
                count++;
            }
        }
        return res;
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值