【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal

解法一:非递归

 1 vector<int> preorderTraversal(TreeNode* root)
 2 {
 3     vector<int> res;
 4     if (root == NULL)
 5         return res;
 6
 7     stack<TreeNode*> node_stack;
 8     TreeNode *p = root;
 9     while (p || !node_stack.empty()) {
10         if (p) {
11             res.push_back(p->val);
12             node_stack.push(p);
13             p = p->left;
14         } else {
15             p = node_stack.top();
16             node_stack.pop();
17             p = p->right;
18         }
19     }
20     return res;
21 }

解法二:递归

 1 void preorderTraversalSub(TreeNode* root, vector<int> &res)
 2 {
 3     if (root == NULL)
 4         return;
 5     res.push_back(root->val);
 6     preorderTraversalSub(root->left, res);
 7     preorderTraversalSub(root->right, res);
 8 }
 9
10 vector<int> preorderTraversal(TreeNode* root)
11 {
12     vector<int> res;
13     preorderTraversalSub(root, res);
14     return res;
15 }
时间: 2024-10-09 11:02:01

【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal的相关文章

[Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal

[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null,2,3] 1 2 / 3 Output: [1,2,3] [思路] 有参考,好机智,使用堆栈压入右子树,暂时存储. 左子树遍历完成后遍历右子树. [代码] /** * Definition for a binary tree node. * public class TreeNode { * in

【LeetCode每天一题】Binary Tree Preorder Traversal(前序遍历)

Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1, null, 1,2,3 ] 1 2 / 3 Output: [1,2,3] Follow up: Recursive solution is trivial, could you do it iteratively? 思路 二叉树的前序遍历方法分为递归法和使用循环辅助栈的方法,递归方法我们在递归左右节点之前先将当

【Leetcode】【Medium】Binary Tree Preorder Traversal

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]. 解题思路: 二叉树非递归先序遍历,使用栈来保存被遍历到的,但是还没遍历其右子树的结点. 代码: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode

【LeetCode从零单刷】Binary Tree Preorder Traversal

题目: Given a binary tree, return the preorder traversal of its nodes' values. Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 解答: 这题难在使用迭代而不是递归.思考一下,先序遍历过程中是不是处理根之后,根的左子树变成新的根,还有子树

【LeetCode从零单刷】Binary Tree Inorder Traversal

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2].  Note: Recursive solution is trivial, could you do it iteratively? 解答: 其实思路有点类似以前的一篇文章:[LeetCode从零单刷]Binary

[LeetCode] Binary Tree Preorder Traversal (非递归的先序遍历)

Binary Tree Preorder Traversal 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 解题思路: 二叉树的前序遍历.

[LeetCode 题解]: Binary Tree Preorder Traversal

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? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

[leetcode]Binary Tree Preorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先序遍历.先序遍历的遍历顺序是:根,左子树,右子树. 解题思路:如果树为下图: 1 /     \ 2         3 /     \    /    \ 4       5  6     7 使用一个栈.步骤为: 一,先遍历节点1,并入栈,如果有左孩子,继续遍历并入栈,一直到栈为{1,2,4}.