LeetCode树与图

Ban

Ban

ChangAn University

二叉树

二叉树基础

二叉树前序遍历

#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/*
前序遍历
*/
void preorder_print(TreeNode *node, int layer)
{
if (!node)
{
return;
}
for (int i = 0; i < layer; i++)
{
printf("-----");
}
printf("[%d]\n", node->val);
preorder_print(node->left, layer + 1);
preorder_print(node->right, layer + 1);
}
int main()
{
TreeNode a(1);
TreeNode b(2);
TreeNode c(5);
TreeNode d(3);
TreeNode e(4);
TreeNode f(6);
a.left = &b;
a.right = &c;
b.left = &d;
b.right = &e;
c.right = &f;
preorder_print(&a, 0);
return 0;
}
//java code
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
class Test {
public static void preorderPrint(TreeNode node, int layer) {
if (node == null) {
return;
}
for (int i = 0; i < layer; i++) {
System.out.print("-----");
}
System.out.format("[%d]\n", node.val);
preorderPrint(node.left, ++layer);
preorderPrint(node.right, ++layer);
}
public static void main(String[] args) {
TreeNode a = new TreeNode(1);
TreeNode b = new TreeNode(2);
TreeNode c = new TreeNode(5);
TreeNode d = new TreeNode(3);
TreeNode e = new TreeNode(4);
TreeNode f = new TreeNode(6);
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.right = f;
preorderPrint(a, 0);
}
}
//伪代码
//中序
preOrderPrint(node.left, ++layer);
System.out.format("[%d]\n", node.val);
preOrderPrint(node.right, ++layer);
//后序
preOrderPrint(node.left, ++layer);
preOrderPrint(node.right, ++layer);
System.out.format("[%d]\n", node.val);

离线API工具Zeal与API文档离线安装

Ban

Ban

ChangAn University

Zeal是是一个离线看API文档的工具,类似macOS上的Dash,支持Windows和Linux。支持200多种语言,包括markdown, JavaSE, JavaEE , C++, C, C#, VUE , JQuery,基本上神马都有。可以按语言搜索,用起来很快。

image-20200313181508664

LeetCode栈、队列与堆

Ban

Ban

ChangAn University

  • 225:使用队列实现栈(Easy)(栈、队列)
  • 使用栈实现队列(East)(栈、队列)
  • 包含min函数的栈(Easy)(栈)
  • 合法的出栈序列(Medium)(栈、队列)
  • 简单的计算器(hard)(栈)
  • 数组中第K大的数(Easy)(堆)
  • 寻找中位数(hard)(堆)

LeetCode链表

Ban

Ban

ChangAn University

链表

链表基础

/*
链表基础
*/
struct ListNode
{
int value;
ListNode *next;
};
int main()
{
ListNode a;
ListNode b;
ListNode c;
ListNode d;
ListNode e;
a.value = 10;
b.value = 20;
c.value = 30;
d.value = 40;
e.value = 50;
a.next = &b;
b.next = &c;
c.next = &d;
d.next = &e;
e.next = NULL;
ListNode *head = &a;
while (head)
{
printf("%d", head->value);
head = head->next;
}
system("pause");
return 0;
}