LeetCode #144 Binary Tree Preorder Traversal 二叉树的前序遍历

144 Binary Tree Preorder Traversal 二叉树的前序遍历

Description:
Given a binary tree, return the preorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]

   1
    \
     2
    /
   3

Output: [1,2,3]

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

题目描述:
给定一个二叉树,返回它的 前序 遍历。

示例 :

输入: [1,null,2,3]

   1
    \
     2
    /
   3 

输出: [1,2,3]

进阶:
递归算法很简单,你可以通过迭代算法完成吗?

思路:

  1. 递归法
    处理 root
    处理 root.left
    处理 root.right
    时间复杂度O(n), 空间复杂度O(n)
  2. 迭代法
    用栈存储
    先压入 root.right
    再压入 root.left
    时间复杂度O(n), 空间复杂度O(n)
    如果采用线索二叉树的方式遍历可以将空间复杂度降为 O(1)

代码:
C++:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution 
{
public:
    vector preorderTraversal(TreeNode* root) 
    {
        vector result;
        TreeNode* cur = root;
        while (cur) 
        {
            if (!cur -> left) 
            {
                result.push_back(cur -> val);
                cur = cur -> right;
            }
            else 
            {
                TreeNode* pre = cur -> left;
                while (pre -> right and pre -> right != cur) pre = pre -> right;
                if (!pre -> right) 
                {
                  result.push_back(cur -> val);
                  pre -> right = cur;
                  cur = cur -> left;
                }
                else
                {
                    pre -> right = nullptr;
                    cur = cur -> right;
                }
            }
        }
        return result;
    }
};

Java:

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

Python:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right) if root else []

你可能感兴趣的:(LeetCode #144 Binary Tree Preorder Traversal 二叉树的前序遍历)