会员
周边
众包
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
卡斯柏的博客
笔下虽有千言,胸中实无一策
卡斯柏的博客
笔下虽有千言,胸中实无一策
博客园
首页
新随笔
联系
订阅
管理
08 2020 档案
30 Day Challenge Day 5 | Leetcode 701. Insert into a Binary Search Tree
摘要:题解 递归和迭代两种解法。 递归解法很直接。 class Solution { public: TreeNode* insertIntoBST(TreeNode* root, int val) { if(!root) return new TreeNode(val); if(val < root->
阅读全文
posted @
2020-08-20 07:42
CasperWin
阅读(139)
评论(0)
推荐(0)
30 Day Challenge Day 4 | Leetcode 102. Binary Tree Level Order Traversal
摘要:题解 典型的BFS模板套路。 class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> ret; if(!root) return ret; queue<TreeNode
阅读全文
posted @
2020-08-19 05:09
CasperWin
阅读(64)
评论(0)
推荐(0)
30 Day Challenge Day 4 | Hackrank - Tree : Top View
摘要:题解 俯视角度下观察结果,我想到用root作为基准,令其序号为0,相应的,左节点在根节点基础上序号减一,而右节点加一。 用一个队列,自上而下逐层遍历。因为观察角度是从上往下,上层的节点会遮住同一列的下层所有节点。一边往下,一遍往左右两侧探索,遇到水平方向更远的节点都要保存到结果中。 void top
阅读全文
posted @
2020-08-19 05:01
CasperWin
阅读(134)
评论(0)
推荐(0)
30 Day Challenge Day 4 | Leetcode 104. Maximum Depth of Binary Tree
摘要:题解 一道easy级别的题,不过适合用来练习BFS的模板套路。面对树结构,通常层级比较明显,如果是遇到图结构,有时则需要转化一下。 class Solution { public: int maxDepth(TreeNode* root) { if(!root) return 0; int dept
阅读全文
posted @
2020-08-19 03:23
CasperWin
阅读(89)
评论(0)
推荐(0)
30 Day Challenge Day 3 | Leetcode 145. Binary Tree Postorder Traversal
摘要:题解 如果是刚做过 144. preorder traversal 的题,这道题不难延伸,虽然这是一道“hard”题。 postorder是“left ==> right ==> root”的遍历顺序,还是应用栈(stack)的数据结构,由于树的遍历必须是自上而下,也就是root必须在前面,不妨把问
阅读全文
posted @
2020-08-18 04:26
CasperWin
阅读(81)
评论(0)
推荐(0)
30 Day Challenge Day 3 | Leetcode 144. Binary Tree Preorder Traversal
摘要:题解 递归的方法很简单,重点是能不能熟练应用非递归的方法。通常根据条件,需要借助队列(queue)或者栈(stack)的结构。 preorder遍历的顺序是“root ==> left ==> right”,而树的遍历是从根节点(root)开始,所以考虑栈的数据结构,因为它实现数据的“先进后出”。
阅读全文
posted @
2020-08-18 03:41
CasperWin
阅读(76)
评论(0)
推荐(0)
公告