589 N叉树前序遍历
给定一个 N 叉树,返回其节点值的前序遍历。
返回其前序遍历:[1, 3, 5, 6, 2, 4].
// 递归: 时间复杂度O(M),M为N叉树中的子节点个数,空间复杂度为O(M),M表示节点个数
class Node {
public int val;
public List<Node> children;
public Node(int val) {
this.val = val;
}
}
List<Integer> list = new ArrayList<>();
public List<Integer> preorder(Node root) {
if (null == root) return list;
list.add(root.val);
for (Node item: root.children) {
preorder(item);
}
return list;
}
// 迭代 : 时间复杂度O(M),M为N叉树中的节点数,每个节点入栈出栈各一次,空间复杂度为O(M),M表示栈的大小
public class Node {
public int val;
public List<Node> children;
public Node(int val) {
this.val = val;
}
}
public List<Integer> preorder(Node root) {
LinkedList<Node> stack = new LinkedList<>();
LinkedList<Integer> output = new LinkedList<>();
if (root == null) return output;
stack.add(root);
while(!stack.isEmpty()) {
Node node = stack.pollLast();
output.add(node.val);
Collections.reverse(node.children);
for (Node item: node.children) {
stack.add(item);
}
}
return output;
}
144 二叉树的前序遍历
给定一个二叉树,返回它的 前序 遍历。
示例
输入: [1,null,2,3]
1
2
/
3
输出: [1,2,3]
// 递归 时间O(n),每个节点都遍历一遍, 空间O(n) 节点个数
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
List<Integer> list = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null)
return list;
list.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
return list;
}
//
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null) return list;
list.add(root.val);
if (root.left != null) {
preorderTraversal(root.left);
}
if (root.right != null) {
preorderTraversal(root.right);
}
return list;
}
}
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
begin_preorderTraversal(root, list);
return list;
}
public void begin_preorderTraversal(TreeNode node, List<Integer> list) {
if (node != null) {
list.add(node.val);
if (node.left != null) {
begin_preorderTraversal(node.left, list);
}
if (node.right != null) {
begin_preorderTraversal(node.right, list);
}
}
}
}
// 迭代 时O(n),空间O(n)
public List<Integer> preorderTraversal(TreeNode root) {
LinkedList<TreeNode> stack = new LinkedList<>();
LinkedList<Integer> output = new LinkedList<>();
if (root == null) return output;
stack.add(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pollLast();
output.add(node.val);
if (node.right != null) {
stack.add(node.right);
}
if (node.left != null) {
stack.add(node.left);
}
}
return output;
}
// 前序
public static void preOrderTraversal(TreeNode head) {
if (head == null) {
return;
}
System.out.print(head.value + " ");
preOrderTraversal(head.left);
preOrderTraversal(head.right);
}
// 中序
public static void inOrderTraversal(TreeNode head) {
if (head == null) {
return;
}
inOrderTraversal(head.left);
System.out.print(head.value + " ");
inOrderTraversal(head.right);
}
// 后续
public static void postOrderTraversal(TreeNode head) {
if (head == null) {
return;
}
postOrderTraversal(head.left);
postOrderTraversal(head.right);
System.out.print(head.value + " ");
}