Java实现 LeetCode 589.N叉树的前序遍历+590.N叉树的后序遍历+429.N叉树的层序遍历

589.N叉树的前序遍历
给定一个 N 叉树,返回其节点值的前序遍历。

例如,给定一个 3叉树 :
在这里插入图片描述

返回其前序遍历: [1,3,5,6,2,4]。

说明: 递归法很简单,你可以使用迭代法完成此题吗?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  1. 递归
/*
// 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> preorder(Node root) {
        List<Integer> ans = new ArrayList<Integer>();
		if(root == null) return ans;
        ans.add(root.val);
		preOrder(ans,root.children);
		return ans;
    }
	private void preOrder(List<Integer> ans, List<Node> children) {
		if(children.isEmpty()) return;
		for(Node node : children) {
			ans.add(node.val);
			preOrder(ans, node.children);
		}
    }
}
  1. 迭代
/*
// 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> preorder(Node root) {
       List<Integer> ans = new ArrayList<Integer>();
        if(root == null) return ans;
        LinkedList<Node> stack = new LinkedList<>();
       stack.add(root);
       while(!stack.isEmpty()) {
    	   Node node = stack.pollLast();
    	   ans.add(node.val);
    	   Collections.reverse(node.children);
    	   for(Node t : node.children) {
    		   stack.add(t);
    	   }
       }
       return ans;
    }
}

590.N叉树的后序遍历
给定一个 N 叉树,返回其节点值的后序遍历。

例如,给定一个 3叉树 :
在这里插入图片描述

返回其后序遍历: [5,6,3,2,4,1].

说明: 递归法很简单,你可以使用迭代法完成此题吗?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  1. 递归
/*
// 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<Integer> ans = new ArrayList<Integer>();
	    if(root == null) return ans;
	    Node node = root;
	    postOrder(ans,root.children);
	    ans.add(node.val);
	    return ans;
	}
	private void postOrder(List<Integer> ans, List<Node> children) {
		if(children.isEmpty()) {
			return;
		}
		for(Node node : children) {
			postOrder(ans,node.children);
			ans.add(node.val);
		}
	
    }
}
  1. 迭代
/*
// 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<Integer> ans = new ArrayList<Integer>();
		if(root == null) return ans;
		Stack<Node> stack = new Stack<>();
		stack.add(root);
		//ans.add(root.val);
		while(!stack.isEmpty()) {
			Node node = stack.pop();
			ans.add(node.val);
			for(Node n : node.children) {
				stack.add(n);
			}
		}
		Collections.reverse(ans);
		return ans;
    }
}

429.N叉树的层序遍历
给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。

例如,给定一个 3叉树 :

在这里插入图片描述

返回其层序遍历:

[
[1],
[3,2,4],
[5,6]
]

说明:

树的深度不会超过 1000。
树的节点总数不会超过 5000。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

/*
// 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<List<Integer>> levelOrder(Node root) {
       List<List<Integer>> ans = new ArrayList<List<Integer>>();
        if(root == null) return ans;
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
        	List<Integer> list = new ArrayList<>();
        	int size = queue.size();
        	for(int i = 0; i < size; i++) {
        		Node node = queue.poll();
        		list.add(node.val);
        		for(Node n : node.children) {
        			queue.add(n);
        		}
        	}
        	ans.add(list);
        }
        return ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值