Leetcode 104二进制树的最大深度。为什么一个迭代解决方案不起作用,而另一个迭代解决方案是

发布于 2025-01-30 15:59:02 字数 1383 浏览 5 评论 0原文

因此,我目前正在解决Leetcode上的104个问题,我遇到了使用BFS实施迭代解决方案的方式,

while(!queue.isEmpty()){                       

            for(int i = 0; i< queue.size(); i++){
                TreeNode node = queue.remove();                
                
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right); 
                }
            }  
            currentLevel++;
            
 }

但是在线查找以下循环,以这种方式进行循环,

while(!queue.isEmpty()){                       
            int size = queue.size();
            while(size -- > 0){
                TreeNode node = queue.remove();                
                
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right); 
                }
            }  
            currentLevel++;
            
        }

我只是想尝试了解为什么对于特定情况,我不起作用,而另一个则是不起作用的。

案件: 树:[0,2,4,1,null,3,-1,5,1,null,6,Null,8]

可视化:

      0
     / \
    2   4
   /   / \
  1   3  -1
 / \   \   \
5   1   6   8

第一个片段结果= 5

秒片段结果= 4

预期结果= 4

So I'm currently solving 104 problem on leetcode and I came across that my way of implementing the iterative solution using BFS doesn't work with the following loop

while(!queue.isEmpty()){                       

            for(int i = 0; i< queue.size(); i++){
                TreeNode node = queue.remove();                
                
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right); 
                }
            }  
            currentLevel++;
            
 }

However looking it up online doing the loop this way DOES work

while(!queue.isEmpty()){                       
            int size = queue.size();
            while(size -- > 0){
                TreeNode node = queue.remove();                
                
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right); 
                }
            }  
            currentLevel++;
            
        }

I'm just trying to understand why for a specific case mine doesn't work but the other one does.

Case:
Tree: [0,2,4,1,null,3,-1,5,1,null,6,null,8]

Visualization:

      0
     / \
    2   4
   /   / \
  1   3  -1
 / \   \   \
5   1   6   8

First snippet result = 5

Second snippet result = 4

Expected result = 4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2025-02-06 15:59:02

您需要考虑迭代条件更改当您在集合上迭代时。

您的迭代条件是队列的初始大小大于零>

...
int size = queue.size();
    while(size -- > 0){
...

第二个实现将初始队列大小放在变量上,并通过不在循环中更改固定的循环迭代数量。

第一个实现在每个环路迭代的开头计算队列大小,但是当您将当前队列大小添加和删除在内部的队列中以进行循环时会变化,这又成为无效的解决方案。

...
for(int i = 0; i< queue.size(); i++){
...

You need to consider iteration condition changes when you iterate over a collection.

Your iteration condition is the initial size of queue being greater than zero.

...
int size = queue.size();
    while(size -- > 0){
...

The second implementation puts the initial queue size to a variable and ensures the fixed number of loop iterations by not changing it inside the loop.

The first implementation calculates queue size at the beginning of each loop iteration, but the current queue size changes as you add and remove values to the queue inside for loop, which in turn becomes an invalid solution.

...
for(int i = 0; i< queue.size(); i++){
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文