Leetcode_#513_找數左下角的值

原題:#513_找數左下角的值

  • 使用隊列保存,按:根-右-左的順序入隊,那麼出隊時,最後一個節點肯定是從右往左數的最後一個節點
public int f (TreeNode root) {
    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(root);
    while (!queue.isEmpty()) {
        TreeNode root = queue.poll();
        if (root.right != null) queue.add(root.right);
        if (root.left != null) queue.add(root.left);
	}
    return root.val;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章