二叉树广度优先遍历和深度优先遍历

https://blog.csdn.net/XTAOTWO/article/details/83625586

广度优先遍历

public List<Integer> bfs(TreeNode root) {
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		List<Integer> list = new LinkedList<Integer>();

		if (root == null) {
			return list;
		}
		queue.add(root);
		while (!queue.isEmpty()) {
			TreeNode treeNode = queue.remove();
			if (null != treeNode.leftNode) {
				queue.add(treeNode.leftNode);
			}
			if (null != treeNode.rightNode) {
				queue.add(treeNode.rightNode);
			}
            list.add(treeNode.val);
		}
		return list;
	}

  

posted @ 2019-12-12 22:17  抽象Java  阅读(599)  评论(0编辑  收藏  举报