145:二叉树的后序遍历
This commit is contained in:
parent
1bbb7c5c09
commit
bd1e12ff7e
@ -0,0 +1,62 @@
|
||||
//给定一个二叉树,返回它的 后序 遍历。
|
||||
//
|
||||
// 示例:
|
||||
//
|
||||
// 输入: [1,null,2,3]
|
||||
// 1
|
||||
// \
|
||||
// 2
|
||||
// /
|
||||
// 3
|
||||
//
|
||||
//输出: [3,2,1]
|
||||
//
|
||||
// 进阶: 递归算法很简单,你可以通过迭代算法完成吗?
|
||||
// Related Topics 栈 树
|
||||
// 👍 564 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//145:二叉树的后序遍历
|
||||
public class BinaryTreePostorderTraversal{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new BinaryTreePostorderTraversal().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> postorderTraversal(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
if(root==null){
|
||||
return list;
|
||||
}
|
||||
list.addAll(postorderTraversal(root.left));
|
||||
list.addAll(postorderTraversal(root.right));
|
||||
list.add(root.val);
|
||||
return list;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<p>给定一个二叉树,返回它的 <em>后序 </em>遍历。</p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> [1,null,2,3]
|
||||
1
|
||||
\
|
||||
2
|
||||
/
|
||||
3
|
||||
|
||||
<strong>输出:</strong> [3,2,1]</pre>
|
||||
|
||||
<p><strong>进阶:</strong> 递归算法很简单,你可以通过迭代算法完成吗?</p>
|
||||
<div><div>Related Topics</div><div><li>栈</li><li>树</li></div></div>\n<div><li>👍 564</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user