首页 > 学院 > 开发设计 > 正文

513:find bottom left tree value

2019-11-08 18:41:54
字体:
来源:转载
供稿:网友

思考:

本题比较简单,是运用广度优先算法,通过队列存储同一深度元素。

	public int findBottleLeftValue(TreeNode root){		Queue<TreeNode> q = new LinkedList<TreeNode>();		q.offer(root);		int result = 0;		while (!q.isEmpty()) {			int size = q.size();			result = q.peek().val;			while (size>0) {				TreeNode tempNode = q.poll();				size--;				if(tempNode.left!=null){					q.offer(tempNode.left);				}				if(tempNode.right!=null){					q.offer(tempNode.right);				}			}		}		return result;	}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表