leetcode 树遍历变种

1、Sum Root to Leaf Numbers
链接:https://leetcode.com/problems/sum-root-to-leaf-numbers/
思路:递归,改层的和 = 上层和* 10 +当前val

    public int sumNumbers(TreeNode root) {
        return sumDFS(root, 0);
    }
    public int sumDFS(TreeNode root, int sum){
        if(root == null)
            return 0;
        sum = sum * 10 + root.val;
        if(root.left == null && root.right == null)
            return sum;
        return sumDFS(root.left, sum) + sumDFS(root.right, sum);
    }

2、Path Sum
链接:https://leetcode.com/problems/path-sum/
思路:递归

    public boolean hasPathSum(TreeNode root, int sum) {
        if(root != null && root.val == sum && root.left == null && root.right == null)
            return true;
        if(root != null){
            return hasPathSum(root.left,sum - root.val) || hasPathSum(root.right,sum - root.val);
        }
        return false;
    }

3、Path Sum II
链接:https://leetcode.com/problems/path-sum-ii/
思路:采用dfs,注意,需要将list删除最后一个值。list中保存中间节点。

    public void dfs(List<List<Integer>> result,TreeNode root, List<Integer> list, int sum) {
        if(root == null)
            return ;
        if(root.val == sum && root.left == null && root.right == null){
            list.add(root.val);
            result.add(new ArrayList(list));//注意需要根据list新new一个值,否则传引用。
            list.remove(list.size() - 1);//删除最后一个叶节点,开始新的遍历
            return;
        }
        list.add(root.val);
        dfs(result,root.left,list, sum - root.val);
        dfs(result,root.right,list, sum - root.val);
        list.remove(list.size() - 1);
    }

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        dfs(result,root, list, sum);
        return result;
    }

4、Maximum Depth of Binary Tree
链接:https://leetcode.com/problems/maximum-depth-of-binary-tree/
思路:递归思想

    public int maxDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
    }

5、Minimum Depth of Binary Tree
链接:https://leetcode.com/problems/minimum-depth-of-binary-tree/
思路:递归,最小深度 = 根节点到叶节点的最小层次

    public static int minDepth(TreeNode root) {
        if (root == null)   return 0;
        if (root.left == null)  return minDepth(root.right) + 1;
        if (root.right == null) return minDepth(root.left) + 1;
        return Math.min(minDepth(root.left),minDepth(root.right)) + 1;
    }

6、Binary Tree Zigzag Level Order Traversal
链接:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
思路:在层次遍历的基础上,增加顺序

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if(root == null)
            return result;
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        boolean flag = false;
        while(!queue.isEmpty()){
            int size = queue.size();
//            List<Integer> list = new ArrayList<>();
            LinkedList<Integer> list = new LinkedList<>();
            flag = flag == true ? false : true;
            for(int i = 0; i < size; i++){
                TreeNode tree = queue.poll();
                if(flag){
                    list.add(tree.val);
                }else{
                    list.addFirst(tree.val);
                }
                if(tree.left != null)
                    queue.add(tree.left);
                if(tree.right != null)
                    queue.add(tree.right);
            }
            result.add(list);
        }
        return result;
    }

7、Populating Next Right Pointers in Each Node
链接:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
思路:层次遍历变形

    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
        Queue<TreeLinkNode> queue = new ArrayDeque<>();
        queue.add(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i = 0; i < size - 1; i++){
                TreeLinkNode tree = queue.poll();
                if(tree.left != null)
                    queue.add(tree.left);
                if(tree.right != null)
                    queue.add(tree.right);
                tree.next = queue.peek();
            }
            TreeLinkNode tree = queue.poll();
            if(tree.left != null)
                queue.add(tree.left);
            if(tree.right != null)
                queue.add(tree.right);
            tree.next = null;
        }
    }

空间复杂度为O(1)的方法:完全二叉树中,start指向每层首个节点,cur遍历该层。

    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
        TreeLinkNode start = root, cur = null;
        while(start.left != null){
            cur = start;
            while(cur != null){
                cur.left.next = cur.right;
                if(cur.next != null){
                    cur.right.next = cur.next.left;
                }
                cur = cur.next;
            }
            start = start.left;

}
    }

8、Populating Next Right Pointers in Each Node II
链接:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
思路:不是完全二叉树,可能缺失左孩子、右孩子。让p指向有孩子的最左节点,letfmost指向最左节点,cur从最左节点开始遍历,如果cur是p的左孩子,则判断是否有右孩子…p后移;如果cur是p的右孩子,p后移;p没有孩子,p后移;cur的next 为p的孩子…

    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
        TreeLinkNode leftmost = root;
        while(leftmost != null){
            TreeLinkNode p = leftmost;
            while(p != null && p.left == null && p.right == null)
                p = p.next;
            if(p == null)
                return;
            leftmost = p.left != null ? p.left :p.right;
            TreeLinkNode cur = leftmost;
            while(p != null){
                if(cur == p.left){
                    if(p.right != null){
                        cur.next = p.right;
                        cur = cur.next;
                    }
                    p = p.next;
                }else if(cur == p.right){
                    p = p.next;
                }else {
                    if(p.left == null && p.right == null){
                        p = p.next;
                        continue;
                    }
                    cur.next = p.left != null ? p.left : p.right;
                    cur = cur.next;
                }
            }

        }
    }

9、Symmetric Tree
链接:https://leetcode.com/problems/symmetric-tree/
思路:判断是都是对称树,比较 n1.val == n2.val ; n1左孩子.val == n2右孩子.val ; n1右孩子.val == n2左孩子.val 依次递推

    public boolean isSymmetric(TreeNode root) {
        if(root == null)
            return true;
        return isPalindrome(root.left, root.right);
    }
    public boolean isPalindrome(TreeNode left,TreeNode right){
        if(left == null && right == null)
            return true;
        if((left != null && right == null) || (left == null && right != null) || (left.val != right.val))
            return false;
        return isPalindrome(left.left,right.right) && isPalindrome(left.right,right.left) ;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章