leetcode559】的更多相关文章

第一次写出了具有迭代和递归的函数,还是有点收获的,虽然题目比较简答 当要对某些对象重复使用时,考虑循环,也就是迭代 当函数可以简化一个重复的操作时,考虑递归,而且就当下一次使用这和函数的结果已经有啦,就不会考虑的太复杂 自己写的答案: """ # Definition for a Node. class Node(object): def __init__(self, val, children): self.val = val self.children = childre…
class Solution { public: int maxDepth(Node* root) { ; if (root != NULL) { queue<Node> Q; Q.push(Node(root->val, root->children)); while (!Q.empty()) { depth++;//层数自增 vector<Node> N; N.clear(); while (!Q.empty()) { Node livenode; livenode…
给定一个 N 叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 说明: 树的深度不会超过 1000. 树的节点总不会超过 5000. class Solution { public: int maxDepth(Node* root) { if(root == NULL) return 0; int cnt = 0; queue<Node*> q; q.push(root); while(!q.empty()) { int s = q.size(); cnt++…
题目: 思路: 直接递归求解最大深度就可以,这里主要记录一下Java中比较获得两个数中最大值的方法. import java.math.*; class Solution { public int maxDepth(Node root) { if(root==null){ return 0; } int deep = 0; for(int i=0; i<root.children.size(); i++){ deep = Math.max(deep,maxDepth(root.children.…
题目 法一.层序遍历 1 class Solution { 2 public: 3 int maxDepth(Node* root) { 4 if(root== NULL) return 0; 5 queue<Node*>q;int level = 0;int lc = 0; 6 q.push(root); 7 while(!q.empty()){ 8 lc = q.size(); 9 for(int i = 0;i < lc;i++){ 10 Node* p = q.front();q…