145:二叉树的后序遍历

This commit is contained in:
huangge1199 2021-04-09 14:49:17 +08:00
parent 1bbb7c5c09
commit bd1e12ff7e
2 changed files with 77 additions and 0 deletions

View File

@ -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)
}

View File

@ -0,0 +1,15 @@
<p>给定一个二叉树,返回它的 <em>后序&nbsp;</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>&nbsp;递归算法很简单,你可以通过迭代算法完成吗?</p>
<div><div>Related Topics</div><div><li></li><li></li></div></div>\n<div><li>👍 564</li><li>👎 0</li></div>