LeetCode- binary tree

Binary tree traversal, recursive and non-recursive attention to two ideas.

94. Binary Tree Inorder Traversal binary preorder

https://leetcode.com/problems/binary-tree-inorder-traversal/

Title: given binary tree, the node returns the value preorder.

Ideas:

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

100. Same Tree same tree

https://leetcode.com/problems/same-tree/

Title: Given two binary tree, write a function to check whether they are identical. If two binary trees are identical in structure, and the nodes have the same value, they are considered identical.

Ideas:

①: to determine whether two trees are identical and symmetrical two trees before judgment is the same principle, using the depth-first search DFS recursively.

②: there are non-recursive solution, because four kinds of binary tree traversal (sequence, the first order, in sequence, after) have their own iteration and recursion wording, let's look at the wording of this iteration of the first order, quite to simultaneously traverse two numbers, each node is then compared.

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) return true;
        if ((p != null && q == null) || (p == null && q != null) || (p.val != q.val)) return false;
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}

129. Sum Root to Leaf Numbers find the root node to the leaf node is the sum of the values

http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/

Title: If a binary tree contains only numbers from 0 to 9, then the root to the leaves of each path may represent a numeric example of a leaf is the root path 1-> 2-> 3, which represents the number 123. To find out the root of the sum of all the number of leaves. Note: leaf node is not a child node.

Ideas:

class Solution {
    int tot = 0;
    public int sumNumbers(TreeNode root) {
        if(root==null)
            return 0;
        calc(root,0);
        return tot;
    }
    
    public void calc(TreeNode root, int currsum) {
        currsum =currsum*10 + root.val;
        if(root.left==null && root.right==null){
            tot+=currsum;
            return;
        }
        if(root.left!=null)
            calc(root.left,currsum);
        if(root.right!=null)
            calc(root.right,currsum);
        return;
    }
}

144. Binary Tree Preorder Traversal before traversing Binary Tree

https://leetcode.com/problems/binary-tree-preorder-traversal/

Title: given binary tree, whose nodes preorder traversal return value.

Ideas:

class Solution {
    // 递归实现
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        if(root == null) return list;
        frontShow(list, root);
        return list;
    }
    
    public void frontShow(List<Integer> l, TreeNode node) {
        l.add(node.val);
        if(node.left != null) {
            frontShow(l, node.left);
        }
        if(node.right != null) {
            frontShow(l, node.right);
        }
    }
}
class Solution {
    // 非递归实现
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<Integer>();
        Stack<TreeNode> rights = new Stack<TreeNode>();
        while(root != null) {
            list.add(root.val);
            if(root.right != null) {
                rights.push(root.right);
            }
            root = root.left;
            if(root == null && !rights.isEmpty()) {
                root = rights.pop();
            }
        }
        return list;
    }
}

145. Binary Tree Postorder Traversal After traversing Binary Tree

https://leetcode.com/problems/binary-tree-postorder-traversal/

Title: given binary tree, the node returns postorder traversal value.

Ideas:

class Solution {
    // 递归实现
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        if(root == null) return list;
        backShow(list, root);
        return list;
    }
    
    public void backShow(List<Integer> l, TreeNode node) {
        if(node.left != null) {
            backShow(l, node.left);
        }
        if(node.right != null) {
            backShow(l, node.right);
        }
        l.add(node.val);
    }
}

// ALL

Traversal variants: http://oj.leetcode.com/problems/path-sum/
traversing variants: http://oj.leetcode.com/problems/path-sum-ii/
traversing variants: HTTP: //oj.leetcode .com / problems / maximum-depth-
of-binary-tree / traverse variants: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/
rebuild binary tree: HTTP: //oj.leetcode .com / problems / construct-binary-
tree-from-preorder-and-inorder-traversal / reconstruction binary tree: http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder- traversal /
traverse the level variants: http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
traversing variants: http://oj.leetcode.com/problems/symmetric-tree/
traversal application:http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/
traversal applications: http://oj.leetcode.com/problems/balanced-binary-tree/
traversal applications: HTTP: // oj.leetcode.com/problems/recover-binary-search-tree/
traversal applications: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/
Level traverse: HTTP: // oj.leetcode.com/problems/binary-tree-level-order-traversal/
Level traverse: http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/
Level traverse variants: HTTP : //oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/
Level traverse variants: http://oj.leetcode.com/problems/populating-next-right-pointers-in -each-node-ii /

Guess you like

Origin www.cnblogs.com/nomad1c/p/11576628.html