LintCode--二叉树

文章目录

    • 66. 二叉树的前序遍历

66. 二叉树的前序遍历

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

首个数据为根节点,后面接着是其左儿子和右儿子节点值,"#"表示不存在该子节点。
节点数量不超过20

样例 1:
输入:{1,2,3}
输出:[1,2,3]
它将被序列化为{1,2,3}
前序遍历

样例 2:
输入:{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: A Tree
     * @return: Preorder in ArrayList which contains node values.
     */
     
    
     
     List<Integer> list = new ArrayList<Integer>();
    public List<Integer> preorderTraversal(TreeNode root) {
        // write your code here
        if(root==null){
            return list;
        }
        list.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return list;
    }
}

代码二:不用递归,还可以怎么写呢?
思想:非递归先序遍历算法基本思路:使用堆栈

  a. 遇到一个节点,访问它,然后把它压栈,并去遍历它的左子树;

  b. 当左子树遍历结束后,从栈顶弹出该节点并将其指向右儿子,继续a步骤;

  c. 当所有节点访问完即最后访问的树节点为空且栈空时,停止。

/**

  • 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: A Tree
* @return: Preorder in ArrayList which contains node values.
*/

 List list = new ArrayList();
public List preorderTraversal(TreeNode root) {
    if(root==null){
        return list;
    }
    Stack stack = new Stack();
    TreeNode current = root;
    while(current!=null || !stack.isEmpty()){
        if(current!=null){
            stack.push(current);
            list.add(current.val);
            current = current.left;
        }else{
            current = stack.pop().right;
        }
    }
    return list;
}
}

也可以这样写:

/**
 * 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: A Tree
     * @return: Preorder in ArrayList which contains node values.
     */
     
    
     
     List<Integer> list = new ArrayList();
    public List<Integer> preorderTraversal(TreeNode root) {
        if(root==null){
            return list;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode current = root;
        while(current!=null || !stack.isEmpty()){
            while(current!=null){
                stack.push(current);
                list.add(current.val);
                current = current.left;
            }
            if(!stack.isEmpty()){
                current = stack.pop().right;
            }
        }
        return list;
    }
}

## 67. 二叉树的中序遍历
描述
给出一棵二叉树,返回其中序遍历。
样例 1:

输入:{1,2,3}
输出:[2,1,3]
解释:
   1
  / \
 2   3
它将被序列化为{1,2,3}
中序遍历
样例 2:

输入:{1,#,2,3}
输出:[1,3,2]
解释:
1
 \
  2
 /
3
它将被序列化为{1,#,2,3}
中序遍历
一、递归代码
```java
/**
 * 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: A Tree
     * @return: Inorder in ArrayList which contains node values.
     */
     
     List<Integer> list = new ArrayList<Integer>();
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root==null){
            return list;
        }
        if(root.left!=null){
            inorderTraversal(root.left);
        }
        list.add(root.val);
        if(root.right!=null){
            inorderTraversal(root.right);
        }
       return list;
        
    }
}

二、非递归代码
```java
/**
 * 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: A Tree
     * @return: Inorder in ArrayList which contains node values.
     */
     
     List<Integer> list = new ArrayList<Integer>();
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root==null){
            return list;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode current = root;
        while(current!=null || !stack.isEmpty()){
            if(current!=null){
                stack.push(current);
                current = current.left;
            }else{
                current = stack.pop();
                list.add(current.val);
                current = current.right;
            }
        }
        return list;
    }
    
}

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

首个数据为根节点,后面接着是其左儿子和右儿子节点值,"#"表示不存在该子节点。
节点数量不超过20。

样例 1:

输入:{1,2,3}
输出:[[1],[2,3]]
解释:
   1
  / \
 2   3
它将被序列化为{1,2,3}
层次遍历
样例 2:

输入:{1,#,2,3}
输出:[[1],[2],[3]]
解释:
1
 \
  2
 /
3
它将被序列化为{1,#,2,3}
层次遍历

代码:使用一个队列,利用size来记录每层的结点数,这是个好方法~~~~
```java
/**
 * 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: A Tree
     * @return: Level order a list of lists of integer
     */
    List<List<Integer>> lists = new ArrayList<>();
    public List<List<Integer>> levelOrder(TreeNode root) {
        if(root==null){
            return lists;
        }
        //巧妙地运用size
        
        Queue<TreeNode> queue= new LinkedList<TreeNode>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new ArrayList<Integer>();
            for(int i=0;i<size;i++){
                TreeNode current = queue.poll();
                list.add(current.val);
                if(current.left!=null){
                    queue.offer(current.left);
                }
                if(current.right!=null){
                    queue.offer(current.right);
                }
            }
            lists.add(list);
        }
        return lists;
    }
}

### 97. 二叉树的最大深度
描述
给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的距离。

样例 1:

输入: tree = {}
输出: 0	
样例解释: 空树的深度是0。
样例 2:

输入: tree = {1,2,3,#,#,4,5}
输出: 3
样例解释: 树表示如下,深度是3
   1
  / \                
 2   3                
    / \                
   4   5
它将被序列化为{1,2,3,#,#,4,5}
```java
/**
 * 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 {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
    
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left, right) + 1;
    }
}


你可能感兴趣的:(算法)