二叉树层序遍历_LeetCode算法102. 二叉树的层序遍历

5fa59846037c91c074525c8997d63b96.png
leetcode

LeetCode算法-102. 二叉树的层序遍历

102. 二叉树的层序遍历

题目

给你一个二叉树,请你返回其按 层序遍历 得到的节点值。(即逐层地,从左到右访问所有节点)。

示例:二叉树:[3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

Java
public List> levelOrder(TreeNode root) {
    List> list = new ArrayList>();if (root == null) {return list;
    }
    Queue queue = new LinkedList();
    queue.add(root);while (!queue.isEmpty()) {int size = queue.size();
        List subList = new ArrayList();for (int i = 0; i 
            TreeNode node = queue.poll();
            subList.add(node.val);if (node.left != null) {
                queue.add(node.left);
            }if (node.right != null) {
                queue.add(node.right);
            }
        }
        list.add(subList);
    }return list;
}
C++
vector<vector<int> > levelOrder1(TreeNode *root) {
    queue q;vectorvector<int> > vv;vector<int> v;if (root){
        v.push_back(root->val);
        vv.push_back(v);
    }
    q.push(root);int i=0;vector vt; while(q.size()>0){
        TreeNode *p = q.front();
        q.pop();
        vt.push_back(p); if ( p==NULL ) {continue;
        }
        q.push(p->left);
        q.push(p->right);
    }int step = 2;int j; for (int i=1; i        v.clear();int cnt=0;for (j=i; j            if (vt[j]) {
                v.push_back(vt[j]->val);
                cnt += 2; 
            }
        } 
        step = cnt;if (v.size()>0) {
            vv.push_back(v);
        }
    }return vv;
}vector<vector<int> > levelOrder2(TreeNode *root) {vectorvector<int> > vv;vector<int> v;if (root){
        v.push_back(root->val);
        vv.push_back(v);
    }vectorvector<int> > vv_left, vv_right;if(root && root->left) {
        vv_left = levelOrder2(root->left);
    }if(root && root->right) {
        vv_right = levelOrder2(root->right);
    }//mergefor(int i=0; i        if (i            vv_left[i].insert(vv_left[i].end(), vv_right[i].begin(), vv_right[i].end());
            vv.push_back(vv_left[i]);
        }else if (i            vv.push_back(vv_left[i]);
        }else {
            vv.push_back(vv_right[i]);
        }
    }return vv;
}vector<vector<int> > levelOrder3(TreeNode *root) {vectorvector<int> > vv;if(root == NULL) return vv;int level = 0; // current level.
    TreeNode *last = root; // last node of currrent level.queue q;
    q.push(root);
    vv.push_back(vector<int>());while(!q.empty()) {
        TreeNode *p = q.front();
        q.pop();
        vv[level].push_back(p->val);if(p->left ) q.push(p->left);if(p->right) q.push(p->right);if(p == last) {
            level++;
            last = q.back();
            vv.push_back(vector<int>()); // new buffer for next row.
        }
    }
    vv.pop_back();return vv;
}

更多干货文章

博客:www.qiuxuewei.com
微信公众号:「@开发者成长之路」
ac4d5faede1566f6bcd7e74322cdb84c.png

「一个没有鸡汤只有干货的公众号」


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值