码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode Binary Tree Preorder Traversal

时间:2015-10-22 06:42:59      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

Given a binary tree, return the preorder traversal of its nodes‘ values.

For example:
Given binary tree {1,#,2,3},

   1
         2
    /
   3

 

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?


解题思路:

Depth-first

There are three types of depth-first traversal: pre-order,[1] in-order,[1] and post-order.[1] For a binary tree, they are defined as display operations recursively at each node, starting with the root node, whose algorithm is as follows:[2] [3]

Pre-order

  1. Display the data part of root element (or current element)
  2. Traverse the left subtree by recursively calling the pre-order function.
  3. Traverse the right subtree by recursively calling the pre-order function.

In-order (symmetric)

  1. Traverse the left subtree by recursively calling the in-order function
  2. Display the data part of root element (or current element)
  3. Traverse the right subtree by recursively calling the in-order function

Post-order

  1. Traverse the left subtree by recursively calling the post-order function.
  2. Traverse the right subtree by recursively calling the post-order function.
  3. Display the data part of root element (or current element).

技术分享Preorder traversal for the given figure is 1 2 4 5 3.

两种方法:1)recursive 2) iterative, 用stack


Java code:

1. recursive

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        if(root != null){
            result.add(root.val);
            result.addAll(preorderTraversal(root.left));
            result.addAll(preorderTraversal(root.right));
        }
        return result;
    }
}

2. iterative

public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<Integer>();
        Stack<TreeNode> s = new Stack<TreeNode>();
        if(root == null){
            return result;
        }
        s.push(root);
        while(!s.isEmpty()){
            TreeNode node = s.pop();
            result.add(node.val);
            if(node.right != null){
                s.push(node.right);
            }
            if(node.left != null){
                s.push(node.left);
            }
        }
        return result;
    }
}

Reference:

1. https://en.wikipedia.org/wiki/Tree_traversal

2. http://www.geeksforgeeks.org/618/

3. https://leetcode.com/discuss/64936/preorder-traversal-java-solution-both-iteration-recursion

 

Leetcode Binary Tree Preorder Traversal

标签:

原文地址:http://www.cnblogs.com/anne-vista/p/4899718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!