Binary Tree的前、中、后序遍历(recursion)

来源:互联网 发布:java服务端开发是什么 编辑:程序博客网 时间:2024/06/14 07:41

都是使用recursion的方式进行计算处理。
以后更新非递归版本 == > 自己模拟stack数据结构处理。
前序遍历:
Given a binary tree, return the preorder traversal of its nodes’ values.

Have you met this question in a real interview? Yes
Example
Given:

1

/ \
2 3
/ \
4 5
return [1,2,4,5,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) {        // write your code here        ArrayList<Integer> array = new ArrayList<>();        if (root == null) {            return array;        }        ArrayList<Integer> left = preorderTraversal(root.left);        ArrayList<Integer> right = preorderTraversal(root.right);        array.add(root.val);        array.addAll(left);        array.addAll(right);        return array;    }}

中序遍历Given a binary tree, return the inorder traversal of its nodes’ values.

Have you met this question in a real interview? Yes
Example
Given binary tree {1,#,2,3},

1
\
2
/
3

return [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) {        // write your code here        ArrayList<Integer> result = new ArrayList<>();        if (root == null) {            return result;        }        ArrayList<Integer> right = inorderTraversal(root.right);        ArrayList<Integer> left = inorderTraversal(root.left);        result.addAll(left);        result.add(root.val);        result.addAll(right);        return result;    }}

后序遍历Given a binary tree, return the postorder traversal of its nodes’ values.

Have you met this question in a real interview? Yes
Example
Given binary tree {1,#,2,3},

1
\
2
/
3

return [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) {        // write your code here        ArrayList<Integer> result = new ArrayList<>();        if (root == null) {            return result;        }        ArrayList<Integer> left = postorderTraversal(root.left);        ArrayList<Integer> right = postorderTraversal(root.right);        result.addAll(left);        result.addAll(right);        result.add(root.val);        return result;    }}
0 0
原创粉丝点击