144:二叉树的前序遍历
This commit is contained in:
parent
f6ebc8cef4
commit
1bbb7c5c09
@ -0,0 +1,99 @@
|
||||
//给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:root = [1,null,2,3]
|
||||
//输出:[1,2,3]
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:root = []
|
||||
//输出:[]
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:root = [1]
|
||||
//输出:[1]
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
//
|
||||
//输入:root = [1,2]
|
||||
//输出:[1,2]
|
||||
//
|
||||
//
|
||||
// 示例 5:
|
||||
//
|
||||
//
|
||||
//输入:root = [1,null,2]
|
||||
//输出:[1,2]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 树中节点数目在范围 [0, 100] 内
|
||||
// -100 <= Node.val <= 100
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:递归算法很简单,你可以通过迭代算法完成吗?
|
||||
// Related Topics 栈 树
|
||||
// 👍 552 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
//144:二叉树的前序遍历
|
||||
public class BinaryTreePreorderTraversal {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new BinaryTreePreorderTraversal().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
if (root == null) {
|
||||
return list;
|
||||
}
|
||||
list.add(root.val);
|
||||
list.addAll(preorderTraversal(root.left));
|
||||
list.addAll(preorderTraversal(root.right));
|
||||
return list;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
<p>给你二叉树的根节点 <code>root</code> ,返回它节点值的 <strong>前序</strong><em> </em>遍历。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg" style="width: 202px; height: 324px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,2,3]
|
||||
<strong>输出:</strong>[1,2,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = []
|
||||
<strong>输出:</strong>[]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1]
|
||||
<strong>输出:</strong>[1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg" style="width: 202px; height: 202px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2]
|
||||
<strong>输出:</strong>[1,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg" style="width: 202px; height: 202px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,2]
|
||||
<strong>输出:</strong>[1,2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点数目在范围 <code>[0, 100]</code> 内</li>
|
||||
<li><code>-100 <= Node.val <= 100</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>递归算法很简单,你可以通过迭代算法完成吗?</p>
|
||||
<div><div>Related Topics</div><div><li>栈</li><li>树</li></div></div>\n<div><li>👍 552</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user