摘要:429. N 叉树的层序遍历 BFS非递归,自己写的。 /* // Definition for a Node. class Node { public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val;
阅读全文
摘要:199. 二叉树的右视图 这道题BFS很容易想到。但DFS没想出来。 这是BFS vector<int> rightSideView(TreeNode* root) { if(root==nullptr) return {}; queue<TreeNode*> qqq; vector<int> re
阅读全文
摘要:103. 二叉树的锯齿形层序遍历 用两个栈来实现。先把根节点放入第一个栈。循环内部根据哪个栈为空判断新的节点放到哪个栈,确定先左还是先右。当两个栈都为空时,循环结束。 /** * Definition for a binary tree node. * struct TreeNode { * int
阅读全文
摘要:102. 二叉树的层序遍历 有两种实现方法。第一种是递归,第二种是队列实现。第一种是看了别人的代码写出来的,第二种是自己写的。这道题的不能直接把遍历得到的数字直接塞进res里,需要区分不同的层次。所以返回的是二维vector 递归 index用来表示这是第几层,要放到哪一个一维数组 class So
阅读全文
摘要:94. 二叉树的中序遍历 迭代法,看别人的代码的,还需要琢磨一下 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNo
阅读全文
摘要:145. 二叉树的后序遍历 class Solution { public: vector<int> res; void Tracking(TreeNode* root) { if(root==nullptr) return; Tracking(root->left); Tracking(root-
阅读全文
摘要:1002. 查找共用字符 class Solution { public: vector<string> commonChars(vector<string>& words) { int size=words.size(); vector<string> res; int alp[26]={0};
阅读全文
摘要:84. 柱状图中最大的矩形 两个星期没写leetcode就连暴力解法都写不出了。 暴力解法 class Solution { public: int largestRectangleArea(vector<int>& heights) { int size=heights.size(),maxSiz
阅读全文