当前位置: 首页 > news >正文

16th Day| 222.完全二叉树的节点个数,110.平衡二叉树,257.二叉树的所有路径,404.左叶子之和

LeetCode 222 完全二叉树的节点个数

题目链接:222.完全二叉树的节点个数

//迭代法
class Solution {public int countNodes(TreeNode root) {if(root == null) return 0;Queue<TreeNode> queue = new LinkedList<>();int count = 0;queue.add(root);while(! queue.isEmpty()){int size = queue.size();count+=size;while(size-- > 0){TreeNode tempNode = queue.poll();if(tempNode.left != null) queue.add(tempNode.left);if(tempNode.right != null) queue.add(tempNode.right);}}return count;     }
}
//普通二叉树后序遍历
class Solution {public int countNodes(TreeNode root) {if(root == null) return 0;int leftNodes = countNodes(root.left);int rightNodes = countNodes(root.right);return leftNodes+rightNodes+1;}
}
//利用完全二叉树的特性
class Solution {public int countNodes(TreeNode root) {if(root == null) return 0;TreeNode left = root.left;TreeNode right = root.right;int leftDepth = 0, rightDepth = 0;while(left != null){left = left.left;leftDepth++;}while(right !=null){right = right.right;rightDepth++;}if(leftDepth == rightDepth){return (2 << leftDepth)-1;}return countNodes(root.left)+countNodes(root.right)+1;}
}

LeetCode 110 平衡二叉树

题目链接:110.平衡二叉树

/**后序遍历 */
class Solution {public boolean isBalanced(TreeNode root) {int result = getHeight(root);return result != -1;}public int getHeight(TreeNode root){if(root == null) return 0;int leftHeight = getHeight(root.left);if(leftHeight == -1) return -1;int rightHeight = getHeight(root.right);if(rightHeight == -1) return -1;if(Math.abs(rightHeight - leftHeight) > 1) return -1;else return Math.max(rightHeight, leftHeight)+1;}
}

LeetCode 257 二叉树的所有路径

题目链接:257.二叉树的所有路径

/**完整版 */
class Solution {public List<String> binaryTreePaths(TreeNode root) {List<String> result = new ArrayList<>();if(root == null) return result;List<Integer> path = new ArrayList<>();searchPaths(root, path, result);return result;}public void searchPaths(TreeNode root, List<Integer> path, List<String> result){path.add(root.val);if(root.left == null && root.right == null){//终止逻辑StringBuilder sb = new StringBuilder();for(int i = 0; i < path.size()-1; i++){sb.append(path.get(i)).append("->");}sb.append(path.get(path.size()-1));result.add(sb.toString());return;}if(root.left != null){searchPaths(root.left, path, result);path.remove(path.size()-1);//回溯}if(root.right != null){searchPaths(root.right, path, result);path.remove(path.size()-1);}    }
}/**简洁版 */
class Solution {List<String> result = new ArrayList<>();public List<String> binaryTreePaths(TreeNode root) {searchPaths(root, "");return result;}public void searchPaths(TreeNode root, String s){if(root == null) return;if(root.left == null && root.right == null){result.add(new StringBuilder(s).append(root.val).toString());}String tmp = new StringBuilder(s).append(root.val).append("->").toString();searchPaths(root.left, tmp);searchPaths(root.right, tmp);}
}

LeetCode 404 左叶子之和

题目链接:404.左叶子之和

/**后序遍历 */
class Solution {public int sumOfLeftLeaves(TreeNode root) {if(root == null) return 0;if(root.left == null && root.right == null) return 0;int leftLeaves = sumOfLeftLeaves(root.left);if(root.left != null && root.left.left ==null && root.left.right == null){leftLeaves = root.left.val;}int rightLeaves = sumOfLeftLeaves(root.right);return leftLeaves+rightLeaves;}
}
/** 层序遍历 迭代法*/
class Solution {public int sumOfLeftLeaves(TreeNode root) {if(root == null) return 0;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);int result = 0;while(! queue.isEmpty()){int size = queue.size();while(size-- > 0){TreeNode tmp = queue.poll();if(tmp.left != null) queue.add(tmp.left);if(tmp.right != null) queue.add(tmp.right);if(tmp.left != null && tmp.left.left == null && tmp.left.right == null){result+=tmp.left.val;}}}return result;}
}
/** 前序遍历 栈模拟递归 迭代法*/
class Solution {public int sumOfLeftLeaves(TreeNode root) {if(root == null) return 0;Stack<TreeNode> stack = new Stack<>();stack.push(root);int result = 0;while(! stack.empty()){TreeNode tmp = stack.pop();if(tmp.right != null) stack.push(tmp.right);if(tmp.left != null) stack.push(tmp.left);if(tmp.left != null && tmp.left.left == null && tmp.left.right == null){result+=tmp.left.val;}}return result;}
}

http://www.dtcms.com/a/268663.html

相关文章:

  • 分布式推客系统架构设计:从微服务到高性能计算的实践路径
  • WebView 中 Cookie 丢失怎么办?跨域状态不同步的调试与修复经验
  • 6,Receiving Messages:@KafkaListener Annotation
  • 诊断工程师进阶篇 --- 车载诊断怎么与时俱进?
  • vue3 字符包含
  • vue openlayer 找出直线上的某一个点 , 点距离直线 最短路径的点 WKT坐标转换为GeoJSON坐标
  • iOS Widget 开发-1:什么是 iOS Widget?开发前的基本认知
  • 亚马逊运营进阶指南:如何用AI工具赋能广告运营
  • 期待在 VR 森林体验模拟中实现与森林的 “虚拟复现”​
  • 华锐视点 VR 污水处理技术对激发学习兴趣的作用​
  • 北京-4年功能测试2年空窗-报培训班学测开-第四十四天
  • UI + MCP Client + MCP Server实验案例
  • 【机器学习笔记 Ⅱ】11 决策树模型
  • Spring Boot 操作 Redis 时 KeySerializer 和 HashKeySerializer 有什么区别?
  • day16——Java集合进阶(Collection、List、Set)
  • Kafka消息积压的原因分析与解决方案
  • 网络安全之重放攻击:原理、危害与防御之道
  • windows grpcurl
  • 用安卓手机给苹果手机设置使用时长限制,怎样将苹果手机的某些APP设置为禁用?有三种方法
  • 软件工程功能点估算基础
  • QML Row与Column布局
  • YOLOv11 架构优化:提升目标检测性能
  • 国内免代理免费使用Gemini大模型实战
  • Vue的生命周期(Vue2)
  • Maven继承:多模块项目高效管理秘笈
  • 微软重磅开源Magentic-UI!
  • 【Rust CLI项目】Rust CLI命令行处理csv文件项目实战
  • AI Tool Calling 实战——让 LLM 控制 Java 工具
  • java-Milvus 连接池(多key)与自定义端点监听设计
  • C++开源项目—2048.cpp