590:N 叉树的后序遍历
This commit is contained in:
parent
7202344824
commit
184bcf0bde
100
src/main/java/leetcode/editor/cn/NAryTreePostorderTraversal.java
Normal file
100
src/main/java/leetcode/editor/cn/NAryTreePostorderTraversal.java
Normal file
@ -0,0 +1,100 @@
|
||||
//给定一个 n 叉树的根节点 root ,返回 其节点值的 后序遍历 。
|
||||
//
|
||||
// n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//输入:root = [1,null,3,2,4,null,5,6]
|
||||
//输出:[5,6,3,2,4,1]
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,
|
||||
//null,13,null,null,14]
|
||||
//输出:[2,6,14,11,7,3,12,8,4,13,9,10,5,1]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 节点总数在范围 [0, 10⁴] 内
|
||||
// 0 <= Node.val <= 10⁴
|
||||
// n 叉树的高度小于或等于 1000
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:递归法很简单,你可以使用迭代法完成此题吗?
|
||||
// Related Topics 栈 树 深度优先搜索 👍 194 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.Node;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//590:N 叉树的后序遍历
|
||||
public class NAryTreePostorderTraversal {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new NAryTreePostorderTraversal().new Solution();
|
||||
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/*
|
||||
// Definition for a Node.
|
||||
class Node {
|
||||
public int val;
|
||||
public List<Node> children;
|
||||
|
||||
public Node() {}
|
||||
|
||||
public Node(int _val) {
|
||||
val = _val;
|
||||
}
|
||||
|
||||
public Node(int _val, List<Node> _children) {
|
||||
val = _val;
|
||||
children = _children;
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public List<Integer> postorder(Node root) {
|
||||
list = new ArrayList<>();
|
||||
dfs(root);
|
||||
return list;
|
||||
}
|
||||
|
||||
List<Integer> list;
|
||||
|
||||
private void dfs(Node root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
if (root.children.size() == 0) {
|
||||
list.add(root.val);
|
||||
return;
|
||||
}
|
||||
for (Node node : root.children) {
|
||||
dfs(node);
|
||||
}
|
||||
list.add(root.val);
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
<p>给定一个 n 叉树的根节点<meta charset="UTF-8" /> <code>root</code> ,返回 <em>其节点值的<strong> 后序遍历</strong></em> 。</p>
|
||||
|
||||
<p>n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 <code>null</code> 分隔(请参见示例)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png" style="height: 193px; width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,3,2,4,null,5,6]
|
||||
<strong>输出:</strong>[5,6,3,2,4,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png" style="height: 269px; width: 296px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
<strong>输出:</strong>[2,6,14,11,7,3,12,8,4,13,9,10,5,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>节点总数在范围 <code>[0, 10<sup>4</sup>]</code> 内</li>
|
||||
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li>n 叉树的高度小于或等于 <code>1000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>递归法很简单,你可以使用迭代法完成此题吗?</p>
|
||||
<div><div>Related Topics</div><div><li>栈</li><li>树</li><li>深度优先搜索</li></div></div><br><div><li>👍 194</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user