第15天 Binary Tree 102、199、116、117、104、111、226、101

层序遍历 102. Binary Tree Level Order Traversal

  • BFS - Queue
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        if (root == null) return ans;

        Queue<TreeNode> q = new LinkedList<TreeNode>();
        q.add(root);
        while (!q.isEmpty()) {
            List<Integer> level = new ArrayList<Integer>();
            int size = q.size();
            for (int i=0; i<size; i++) {
                TreeNode cur = q.poll();
                level.add(cur.val);
                if (cur.left != null) q.add(cur.left);
                if (cur.right != null) q.add(cur.right);
            }
            ans.add(level);
        }

        return ans;
    }
}
  • DFS - Recursion
class Solution {
    List<List<Integer>> ans = new ArrayList<>();

    public List<List<Integer>> levelOrder(TreeNode root) {
        helper(root, 0);
        return ans;
    }

    public void helper(TreeNode root, int i) {
        if (root == null) return;

        if (ans.size() == i)
            ans.add(new ArrayList<>());

        ans.get(i).add(root.val);

        if (root.left != null) helper(root.left, i+1);
        if (root.right != null) helper(root.right, i+1);
    }
}

199. Binary Tree Right Side View

116. Populating Next Right Pointers in Each Node

  • Queue
  • 每一level,先把第一个拿出来做cur,后面的正常进for loop,cur.next = temp, cur = temp更新
  • Time Complexity: O(N)
  • Space Complexity: O(N) 
class Solution {
    public Node connect(Node root) {
        if (root == null) return null;

        Queue<Node> q = new LinkedList<Node>();
        q.add(root);
        while (!q.isEmpty()) {
            int size = q.size();
            Node cur = q.poll();
            if (cur.left != null) q.add(cur.left);
            if (cur.right != null) q.add(cur.right);

            for (int i=1; i<size; i++) {
                Node temp = q.poll();
                cur.next = temp;
                if (cur.left != null) q.add(temp.left);
                if (cur.right != null) q.add(temp.right);
                cur = temp;
            }
        }

        return root;
    }
}

117. Populating Next Right Pointers in Each Node II

  • Queue
  • 建立指针的时候可以用
if (i<size-1) cur.next = q.peek();
class Solution {
    public Node connect(Node root) {
        if (root == null) return null;

        Queue<Node> q = new LinkedList<Node>();
        q.add(root);
        while (!q.isEmpty()) {
            int size = q.size();
            for (int i=0; i<size; i++) {
                Node cur = q.poll();
                if (i<size-1) cur.next = q.peek();
                if (cur.left != null) q.add(cur.left);
                if (cur.right != null) q.add(cur.right);
            }
        }

        return root;
    }
}

104. Maximum Depth of Binary Tree

  • 方法1: Recursion
  • 方法2: Queue

111. Minimum Depth of Binary Tree

  • 方法1: DFS - Recursion 左右中
  • 注意两个base cases
  • 注意每一层更新depth要考虑左右child是否为null
class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        if (root.left == null && root.right == null) return 1;

        int leftDepth = Integer.MAX_VALUE, rightDepth = Integer.MAX_VALUE;
        if (root.left!=null)
            leftDepth = minDepth(root.left)+1;
        if (root.right != null)
            rightDepth = minDepth(root.right)+1;
        return Math.min(leftDepth, rightDepth);
    }
}
  • 方法2: Queue

226. 翻转二叉树 Invert Binary Tree

  • 方法1: DFS recursion - Preorder, Postorder 都可以
  • recursion非base case情况,也要return root,这样一层一层上去
  • Time Complexity: O(n)
  • Space Complexity: O(n). O(h) function calls will be placed on the stack in the worst case, where h is the height of the tree. Because h∈O(n), the space complexity is O(n).
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) return root;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}
  • 方法2: BFS Queue
  • Time Complexity: O(n)
  • Space Complexity: O(n). Worst Case: For a full binary tree, the leaf level has ⌈n/2⌉=O(n) leaves.

101. 对称二叉树 Symmetric Tree

  • 方法1: DFS recursion
  • 正是因为要遍历两棵树而且要比较内侧和外侧节点,所以准确的来说是一个树的遍历顺序是左右中,一个树的遍历顺序是右左中。-> 要用helper compare function
  • 1. 确定递归函数的参数和返回值
compare(TreeNode left, TreeNode right)
  • 2. 确定终止条件
  • 3. 确定单层递归的逻辑
  • Time Complexity: O(n)
  • Space Complexity: The number of recursive calls is bound by the height of the tree. In the worst case, the tree is linear and the height is in O(n). 
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        return compare(root.left,root.right);
    }

    public boolean compare(TreeNode left, TreeNode right) {
        if (left == null && right == null) return true;
        if (left != null && right == null) return false;
        if (left == null && right != null) return false;
        if (left.val != right.val) return false;

        return compare(left.left, right.right) && compare(left.right, right.left);
    }
}
  • 方法2:BFS Queue
  • Time Complexity: O(n)
  • Space Complexity: O(n)
public boolean isSymmetric(TreeNode root) {
    Queue<TreeNode> q = new LinkedList<>();
    q.add(root);
    q.add(root);
    while (!q.isEmpty()) {
        TreeNode t1 = q.poll();
        TreeNode t2 = q.poll();
        if (t1 == null && t2 == null) continue;
        if (t1 == null || t2 == null) return false;
        if (t1.val != t2.val) return false;
        q.add(t1.left);
        q.add(t2.right);
        q.add(t1.right);
        q.add(t2.left);
    }
    return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值