Leetcode学习笔记:#513. Find Bottom Left Tree Value

Leetcode学习笔记:#513. Find Bottom Left Tree Value

Given a binary tree, find the leftmost value in the last row of the tree.

实现:

public int findLeftMostNode(TreeNode root){
	Queue<TreeNode> queue = new LinkedList<>();
	queue.add(root);
	while(!queue.empty()){
		root = queue.poll();
		if(root.right != null)
			queue.add(root.right);
		if(root.left != null)
			queue.add(root.left);
	}
	return root.val;

思路:
BFS,利用队列, 从右到左遍历,因为队列是先进先出,所以最后一个进入队列的必定是最后一个节点

猜你喜欢

转载自blog.csdn.net/ccystewart/article/details/90129167
今日推荐